package com.tom.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONPath;

import com.tom.config.Channel;
import com.tom.config.CustomResponse;
import com.tom.constant.ArticleEnum;
import com.tom.dto.ArticleContentDTO;
import com.tom.entity.*;
import com.tom.mapper.bwarticle.ArticleMapper;
import com.tom.mapper.opencms.PrimePeriodicalMapper;
import com.tom.mapper.opencms.TomArticleMapper;
import com.tom.service.ArticleRemoteService;
import com.tom.service.CMSArticleApi;
import com.tom.service.IArticleService;
import com.tom.service.external.BusinessWeeklyService;
import com.tom.vo.ArticleVO;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @version V1.0
 * @Package bw-article-api
 * @Description:
 * @author: dengyi
 * @date: 2020/2/10
 * @Copyright: 2019 www.tomonline-inc.com Inc. All rights reserved.
 * 注意：本内容仅限于TOM集团内部传阅，禁止外泄以及用于其他的商业目
 */
@Slf4j
@Service
public class ArticleImpl implements IArticleService {
    @Value("${bw-article.listurl}")
    private String getArticleList;
    @Value("${bw-article.type}")
    private String type;
    @Value("${bw-article.SiteNo}")
    private String siteNo;
    @Value("${bw-article.url}")
    private String getArticle;
    @Value("${bw-article.IMG_SERVER_URL}")
    private String imgServerurl;
    @Value("${bw-article.Directoryprefix}")
    private String prefix;
    @Value("${bw-article.TIME_LIMIT}")
    private long timeLimit;
    @Value("${bw-article.CHARGE_ARTICLE_YEAR_BOUNDARIES}")
    private int boundariesYear;
    @Value("${bw-article.GET_MAGAZINE_LIST}")
    private String getMagazineList;
    @Autowired
    private Channel channel;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private PrimePeriodicalMapper primePeriodicalMapper;
    @Autowired
    private com.tom.service.impl.Service service;

    @Autowired
    private TomArticleMapper tomArticleMapper;

    @Autowired
    private BusinessWeeklyService businessWeeklyService;

    @Autowired
    private CMSArticleApi articleApi;

    @Autowired
    private ArticleRemoteService articleRemoteService;

    //参数type:
    //1:頻道文章、3:全站最新文章、4:免費最新文章、5:限時免費最新文章 、6:付費最新文章 、7:雜誌最新文章 、8:音頻最新文章、9:即時觀點最新文章、
    //10:國際媒體最新文章、11:全站熱門文章、12:免費熱門文章、13:限時免費熱門文章、14:付費熱門文章、15:雜誌熱門文章、16:即時觀點熱門文章、
    // 17:國際媒體熱門文章、18:音頻熱門文章、19:專欄最新文章、20:專欄熱門文章、21:廣編文章、22:多媒體最新文章

    /**
     * 免费文章生成json文件入口
     */
    @Override
    public void listArticle() {
        log.info("+++++++++++++++++++++++++免费文章读取开始");
        //调用文件列表接口
        for (String id : channel.getMap().keySet()) {
            boolean goon = true;
            /*int End = 100;
            int Start = 0;
            do {*/
            HttpRequest httpRequest = HttpRequest.post(getArticleList);
            httpRequest.form("SiteNo", siteNo)//10:商周.Com、 16:IOS-APP、17:Andriod-APP
                    .form("Type", type)//
                    .form("ID", id)
                    .form("Start", 1)//开始的RankingNo
                    .form("End", 100)//结束的RankingNo
                    .form("Status", "y")//
                    .form("SortType", "3")//3為新到舊，4為舊到新
                    .form("cache", "n");//是否从缓存里面取，默认从缓存里面取
            HttpResponse response = httpRequest.send();
            Object extract = JSONPath.extract(response.bodyText(), "$.JData.Data");
            List<Article> articlesList = ((JSONArray) extract).toJavaList(Article.class);
            log.info("调用" + getArticleList + "返回值个数：{}", articlesList.size());
            String channelname = channel.getMap().get(id);
            for (Article article : articlesList) {
                long currentTime = 0;
                try {
                    log.info(":{}", article.getNewDataTime());
                    String format = DateFormatUtils.format(DateUtils.parseDate(article.getNewDataTime(), "yyyy-MM-dd'T'HH:mm:ss"), "yyyy-MM-dd");
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    currentTime = dateFormat.parse(format).getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (currentTime >= timeLimit) {
                    log.info("时间通过了:{}", article.getNewDataTime());
                    getArticleContent(channelname, String.valueOf(article.getDataTab()), article.getID(), siteNo, "2", true);
                }
            }
                /*int count = (int) JSONPath.extract(response.bodyText(), "$.JData.TotalCount");
                if (End >= count) {
                    log.info(channelname + "频道获取到的总数:{}", count);
                    goon = false;
                } else {
                    log.info("正在取" + channelname + "这个频道的" + Start + "-" + End + "数据");
                    Start = End;
                    End = End + 100;
                }
            } while (goon);*/
        }
    }

    @Override
    public void chargeListArticle() {
        log.info("============================付费文章读取开始=====================");
        int journalStart = -1;
        int journalEnd = 1000;
        int End = 100;
        int Start = 0;
        HttpRequest httpRequest = HttpRequest.post("https://api.businessweekly.com.tw/api/Content/GetMagazineList");
        httpRequest.form("SiteNo", siteNo)//10:商周.Com、 16:IOS-APP、17:Andriod-APP
                .form("Year", Calendar.getInstance().get(Calendar.YEAR))
                .form("Start", journalStart)//开始的RankingNo
                .form("End", journalEnd)//结束的RankingNo
                .form("cache", "n");//是否从缓存里面取，默认从缓存里面取
        HttpResponse response = httpRequest.send();
        Object extract = JSONPath.extract(response.bodyText(), "$.JData");
        List<MagazineList> magazineList = ((JSONArray) extract).toJavaList(MagazineList.class);
        // 1. 获取最新期刊
        String issueNumber = magazineList.stream().max(Comparator.comparing(item -> item.getIssueNumber())).get().getIssueNumber();

        // 2. 获取最新期刊文章列表
        List<Article> magazineArticleList = articleApi.getMagazineArticleList(siteNo, issueNumber, Start, End);
        for (Article article : magazineArticleList) {
            // 3. 付费文章的频道id需要到文章详情里面取
            articleRemoteService.getArticleContent(String.valueOf(article.getDataTab()), article.getID(), siteNo, "2");
        }
    }

    /**
     * 付费文章入口
     */
    @Override
    public void chargeListArticle(Integer year, Integer start, Integer end) {
        log.info("付费文章读取开始");
        int End = 100;
        int Start = 0;
        HttpRequest httpRequest = HttpRequest.post("https://api.businessweekly.com.tw/api/Content/GetMagazineList");
        httpRequest.form("SiteNo", siteNo)//10:商周.Com、 16:IOS-APP、17:Andriod-APP
                .form("Year", year)
                .form("Start", start)//开始的RankingNo
                .form("End", end)//结束的RankingNo
                .form("cache", "n");//是否从缓存里面取，默认从缓存里面取
        HttpResponse response = httpRequest.send();
        Object extract = JSONPath.extract(response.bodyText(), "$.JData");
        List<MagazineList> MagazineList = ((JSONArray) extract).toJavaList(MagazineList.class);
        for (MagazineList magazine : MagazineList) {
            boolean goon = true;
            String IssueNumber = magazine.getIssueNumber();
            do {
                HttpRequest httpRequestcharge = HttpRequest.post(getArticleList);
                httpRequestcharge.form("SiteNo", siteNo)//10:商周.Com、 16:IOS-APP、17:Andriod-APP
                        .form("Type", "7")//
                        .form("ID", IssueNumber)
                        .form("Start", Start)//开始的RankingNo
                        .form("End", End)//结束的RankingNo
                        .form("Status", "y")//
                        .form("SortType", "3")//3為新到舊，4為舊到新
                        .form("cache", "n");//是否从缓存里面取，默认从缓存里面取
                HttpResponse responsecharge = httpRequestcharge.send();
                Object chargeExtract = JSONPath.extract(responsecharge.bodyText(), "$.JData.Data");
                List<Article> articlesList = ((JSONArray) chargeExtract).toJavaList(Article.class);

                for (Article article : articlesList) {
                    //付费文章的频道id需要到文章详情里面取
                    getArticleContent("", "4", article.getID(), siteNo, "2", false);
                }
                int count = (int) JSONPath.extract(responsecharge.bodyText(), "$.JData.TotalCount");
                if (End >= count) {
                    log.info("付费期刊号为:{}的文章获取到的总数:{}", IssueNumber, count);
                    goon = false;
                } else {
                    log.info("正在取收费文章期刊号为" + IssueNumber + "的" + Start + "-" + End + "数据");
                    Start = End;
                    End = End + 100;
                }
            } while (goon);
        }
    }

    @Override
    public void chargeListArticleNotSkip(Integer year, Integer start, Integer end, String provenance) {
        log.info("付费文章读取开始");
        int End = 100;
        int Start = 0;
        HttpRequest httpRequest = HttpRequest.post("https://api.businessweekly.com.tw/api/Content/GetMagazineList");
        httpRequest.form("SiteNo", siteNo)//10:商周.Com、 16:IOS-APP、17:Andriod-APP
                .form("Year", year)
                .form("Start", start)//开始的RankingNo
                .form("End", end)//结束的RankingNo
                .form("cache", "n");//是否从缓存里面取，默认从缓存里面取
        HttpResponse response = httpRequest.send();
        Object extract = JSONPath.extract(response.bodyText(), "$.JData");
        List<MagazineList> MagazineList = ((JSONArray) extract).toJavaList(MagazineList.class);
        for (MagazineList magazine : MagazineList) {
            boolean goon = true;
            String IssueNumber = magazine.getIssueNumber();
            do {
                HttpRequest httpRequestcharge = HttpRequest.post(getArticleList);
                httpRequestcharge.form("SiteNo", siteNo)//10:商周.Com、 16:IOS-APP、17:Andriod-APP
                        .form("Type", "7")//
                        .form("ID", IssueNumber)
                        .form("Start", Start)//开始的RankingNo
                        .form("End", End)//结束的RankingNo
                        .form("Status", "y")//
                        .form("SortType", "3")//3為新到舊，4為舊到新
                        .form("cache", "n");//是否从缓存里面取，默认从缓存里面取
                HttpResponse responsecharge = httpRequestcharge.send();
                Object chargeExtract = JSONPath.extract(responsecharge.bodyText(), "$.JData.Data");
                List<Article> articlesList = ((JSONArray) chargeExtract).toJavaList(Article.class);

                for (Article article : articlesList) {
                    //付费文章的频道id需要到文章详情里面取
                    getArticleContent("", "4", article.getID(), siteNo, provenance, false);
                }
                int count = (int) JSONPath.extract(responsecharge.bodyText(), "$.JData.TotalCount");
                if (End >= count) {
                    log.info("付费期刊号为:{}的文章获取到的总数:{}", IssueNumber, count);
                    goon = false;
                } else {
                    log.info("正在取收费文章期刊号为" + IssueNumber + "的" + Start + "-" + End + "数据");
                    Start = End;
                    End = End + 100;
                }
            } while (goon);
        }
    }

    /**
     * 付费文章不提跳过
     */

    @Override
    public void digitalNative() {
        int total;
        int processed = 0; // 已处理文章数
        int start = 1;
        int step = 100;
        int end = step;
        log.info("开始抓取数位原生稿...");
        do {
            String responseBody = businessWeeklyService.fetchDigitalNative(siteNo, start, end);

            // 提取总数帮助循环正常进行
            total = (int) JSONPath.extract(responseBody, "$.JData.TotalCount");

            // 提取文章数据
            Object extract = JSONPath.extract(responseBody, "$.JData.Data");
            List<Article> articles = ((JSONArray) extract).toJavaList(Article.class);

            for (Article article : articles) {
                getArticleContent("", String.valueOf(article.getDataTab()), article.getID(),
                        siteNo, "2", true);
                processed++;
            }
            start = end + 1;
            end += step;
        } while (processed < total);
        log.info("数位原生稿抓取完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String deleteJsonTransitionHtmlFail(TomArticle tomArticle) {
        String messages = "";
        if (tomArticle == null || tomArticle.getArticlepath() == null) {
            return "參數為空";
        }

        try {
            String articlePath = tomArticle.getArticlepath();
            //删除tom_article
            tomArticleMapper.deleteTomArticle(articlePath);
            //删除tom_content_json
            tomArticleMapper.deleteTomContentJson(articlePath);
            //删除CMS_ONLINE_STRUCTURE
            String resouceId = tomArticleMapper.selectResouceIdByResoucePath(articlePath);
            tomArticleMapper.deleteCMS_ONLINE_STRUCTURE(resouceId);
            //删除CMS_OFFLINE_PROPERTIES
            tomArticleMapper.deleteCMS_OFFLINE_PROPERTIES(resouceId);
            //删除DELETE FROM  CMS_OFFLINE_RESOURCES WHERE RESOURCE_ID=
            tomArticleMapper.deleteCMS_OFFLINE_RESOURCES(resouceId);
            //删除DELETE FROM CMS_OFFLINE_CONTENTS  WHERE RESOURCE_ID=
            tomArticleMapper.deleteCMS_OFFLINE_CONTENTS(resouceId);
            //删除DELETE FROM  CMS_OFFLINE_ACCESSCONTROL  WHERE RESOURCE_ID=
            tomArticleMapper.deleteCMS_OFFLINE_ACCESSCONTROL(resouceId);
            //删除DELETE FROM CMS_OFFLINE_STRUCTURE WHERE RESOURCE_ID=
            tomArticleMapper.deleteCMS_OFFLINE_STRUCTURE(resouceId);
            //删除DELETE FROM CMS_ONLINE_RESOURCES WHERE RESOURCE_ID=
            tomArticleMapper.deleteCMS_ONLINE_RESOURCES(resouceId);
            messages += "删除成功";

        } catch (Exception e) {
            messages += "删除失败===>" + e;
        }
        //删除
        return messages;
    }

    @Override
    public String updateArticleByDate(String startDate, String endDate) {
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            return "參數為空";
        }
        final List<String> ps = tomArticleMapper.selectParamByPublish(startDate, endDate);
        if (ps == null || ps.isEmpty()) {
            return "無數據";
        }
        StringBuilder messages = new StringBuilder();
        for (String p : ps) {
            Map<String, String> params = new LinkedHashMap<>();
            for (String param : p.split("&")) {
                String[] keyValue = param.split("=", 2);
                String key;
                try {
                    key = URLDecoder.decode(keyValue[0], "UTF-8");
                    String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], "UTF-8") : "";
                    if (!key.isEmpty()) {
                        params.put(key, value);
                    }
                } catch (UnsupportedEncodingException ignored) {
                    // do nothing
                }
            }
            String message = getArticleContent("", params.get("Type"), params.get("ArticleNo"), params.get("SiteNo"), "1", false);
            System.out.println("id:" + params.get("ArticleNo") + "-" + message + "; ");
            if (!StringUtils.contains(message, "成功")) {
                messages.append("id:").append(params.get("ArticleNo")).append("-").append(message).append("; ");
            }
        }
        return messages.toString();
    }


    @Override
    public String updateArticleByDateAndType(String startDate, String endDate, Integer type) {
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            return "參數為空";
        }
        final List<String> ps = tomArticleMapper.selectParamByPublishAndType(startDate, endDate, type);
        if (ps == null || ps.isEmpty()) {
            return "無數據";
        }
        StringBuilder messages = new StringBuilder();
        for (String p : ps) {
            Map<String, String> params = new LinkedHashMap<>();
            // TODO 这个地方要判空
            if (p == null || p.equals("")) {
                //要记录
                tomArticleMapper.insertFailureArticle(p, new Date(), "accessParameters为null或空");
                continue;
            }
            for (String param : p.split("&")) {
                String[] keyValue = param.split("=", 2);
                String key;
                try {
                    key = URLDecoder.decode(keyValue[0], "UTF-8");
                    String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], "UTF-8") : "";
                    if (!key.isEmpty()) {
                        params.put(key, value);
                    }
                } catch (UnsupportedEncodingException ignored) {
                    // do nothing
                }
            }
            String message = getArticleContent("", params.get("Type"), params.get("ArticleNo"), params.get("SiteNo"), "1", false);
            System.out.println("id:" + params.get("ArticleNo") + "-" + message + "; ");
            if (!StringUtils.contains(message, "成功")) {
                messages.append("id:").append(params.get("ArticleNo")).append("-").append(message).append("; ");
            }
        }
        return messages.toString();
    }

    @Override
    public String updateDBArticleByDate(String startDate, String endDate) {
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            return "參數為空";
        }
        final List<String> ps = tomArticleMapper.selectParamByPublishNotContentType(startDate, endDate);
        if (ps == null || ps.isEmpty()) {
            return "無數據";
        }
        StringBuilder messages = new StringBuilder();
        for (String p : ps) {
            Map<String, String> params = new LinkedHashMap<>();
            // TODO 这个地方要判空
            if (p == null || p.equals("")) {
                //要记录
                tomArticleMapper.insertFailureArticle(p, new Date(), "accessParameters为null或空");
                continue;
            }
            for (String param : p.split("&")) {
                String[] keyValue = param.split("=", 2);
                String key;
                try {
                    key = URLDecoder.decode(keyValue[0], "UTF-8");
                    String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], "UTF-8") : "";
                    if (!key.isEmpty()) {
                        params.put(key, value);
                    }
                } catch (UnsupportedEncodingException ignored) {
                    // do nothing
                }
            }
            ArticleContentDTO Content;
            HttpRequest httpRequest = null;
            ArticleEnum articleStatus;
            String accessParameters;//调用文章详情的时用到的参数
            try {

                httpRequest = HttpRequest.post(getArticle);
                httpRequest.timeout(1000 * 60);
                httpRequest
                        .form("SiteNo", params.get("SiteNo"))//
                        .form("Type", params.get("Type"))//
                        .form("ArticleNo", params.get("ArticleNo"))
                        .form("cache", "n");//
                accessParameters = "Type=" + params.get("Type") + "&ArticleNo=" +  params.get("ArticleNo");
                log.info("查询文章详情:{} ;文章DataTab=:{}", httpRequest, params.get("Type"));
                HttpResponse response = httpRequest.send();
                String articleContent = response.bodyText();
                Content = JSON.parseObject(articleContent, ArticleContentDTO.class);
                if (Content==null||Content.getJData()==null||Content.getJData().getArticle()==null){
                    log.info("updateDBArticleByDate找不到文章:{} ",p);
                    continue;
                }
                tomArticleMapper.updateContentType(p,Content.getJData().getArticle().getContentType());
                Thread.sleep(500);
            } catch (Exception e) {
                log.error("获取文章详情出错:{}", httpRequest, e);
                return "获取文章详情失败";
            }
        }
        return messages.toString();
    }

    /**
     * 获取文章详情
     *
     * @param channelname 频道名
     * @param DataTab     类型
     * @param ID          文章ID
     * @param siteNo      10，16，17
     * @param provenance  状态（1-更新；2-新增）
     * @return 返回消息
     */
    @Override
    public String getArticleContent(String channelname, String DataTab, String ID, String siteNo, String provenance, boolean DateLimit) {
        ArticleContentDTO Content;
        HttpRequest httpRequest = null;
        ArticleEnum articleStatus;
        String accessParameters;//调用文章详情的时用到的参数
        try {

            httpRequest = HttpRequest.post(getArticle);
            httpRequest.timeout(1000 * 60);
            httpRequest
                    .form("SiteNo", siteNo)//
                    .form("Type", DataTab)//
                    .form("ArticleNo", ID)
                    .form("cache", "n");//
            accessParameters = "Type=" + DataTab + "&ArticleNo=" + ID;
            log.info("查询文章详情:{} ;文章DataTab=:{}", httpRequest, DataTab);
            HttpResponse response = httpRequest.send();
            String articleContent = response.bodyText();
            Content = JSON.parseObject(articleContent, ArticleContentDTO.class);
        } catch (Exception e) {
            log.error("获取文章详情出错:{}", httpRequest, e);
            return "获取文章详情失败";
        }
        //判断文章是不是已经录入过,如果录入过放回之前的json文件的保存路径
        if (Content.getErrorCode() != 1) {
            log.error("获取不到文章详情");
            return "获取不到文章详情";
        }
        //判断发布时间是不是小于等于当前时间，不是就不生产json文件
        if (DateLimit) {
            Date date = null;
            try {
                date = DateUtils.parseDate(Content.getJData().getArticle().getNewDataTime(), "yyyy-MM-dd'T'HH:mm:ss");
            } catch (ParseException e) {
                log.error("");
            }
            if (date != null && date.getTime() > System.currentTimeMillis()) {
                return "发布时间大于当前时间";
            }
        }
        String articleJsonPath = JudgeArticleStatus(Content, provenance, accessParameters);
        articleStatus = articleJsonPath == null ? ArticleEnum.NOT_EXIST_ANY_DATABASE : ArticleEnum.EXIST_IN_ARTICLE;

        if (StringUtils.isEmpty(articleJsonPath)) {
            articleJsonPath = queryPathInOpencms(Content, accessParameters);
            articleStatus = articleJsonPath == null ? articleStatus : ArticleEnum.EXIST_IN_TOM_ARTICLE;
        }

        if (StringUtils.isNotEmpty(articleJsonPath) && "2".equals(provenance)) {
            // provenance 状态为2只做新增，不做更新
            return "已存在";
        }


        //转化成json格式
        //如果二级频道为空就取一级频道
        String channelID = Content.getJData().getArticle().getWebSubCategoryZeroID();
        if (StringUtil.isEmpty(channelID)) {
            channelID = Content.getJData().getArticle().getWebCategoryZeroID();
        }
        channelname = channel.getMap().get(channelID);
        if (StringUtil.isEmpty(channelname)) {
            articleMapper.insertlog("付费文章生成json时没有找到对应的频道名称的频道id:" + channelID, ID);
            log.info("没有找到对应的频道名称的频道id:" + channelID);
            return "没有找到对应的频道名称";
        }

        accessParameters = "SiteNo=10&" + accessParameters;
        ArticleJSON articleJSON = createJSON(Content, channelname, accessParameters, DataTab);
        if (articleJSON == null) {
            return "生成json文件失败";
        }
        String json = JSON.toJSONString(articleJSON);
        try {
            StringBuilder storePath = new StringBuilder();
            if (StringUtils.isEmpty(articleJsonPath)) {
                //新增
                //文件名(1+文章id_期刊号.json)收費文章的命名
                //查看之前的代碼，是這樣明明的，有衝突，最后决定与之前代码保持一致
                //文件名拼接
                StringBuilder fileName = new StringBuilder();
                if (!DataTab.equals("4")) {
                    Long id = ((System.currentTimeMillis()) % (964000000));
                    String ids = String.format("%09d", id);
                    fileName.append("1").append(ids).append(".json");
                } else {
                    fileName.append("1")
                            .append(Content.getJData().getArticle().getID())
                            .append("_")
                            .append(Content.getJData().getArticle().getMagazineIssueNumber())
                            .append(".json");
                    final String magazineIssueNumber = Content.getJData().getArticle().getMagazineIssueNumber();
                    if (StringUtils.isNotBlank(magazineIssueNumber)) {
                        // 期刊信息添加到opencms库的tom_prime_periodical表
                        insertTomPrimePeriodical(Integer.parseInt(magazineIssueNumber));
                    }
                }
                //文件存放路径：/data/spiderjson/+频道名称+/content/
                storePath.append(prefix)
                        .append(channelname)
                        .append("/content/").append(fileName);
                log.info("json的保存路径:{}", storePath);
            } else {
                //更新
                storePath.append(articleJsonPath);
                log.info("json的更新路径:{}", storePath);
            }
            //保存到数据库
            int message = 0;
            if (articleStatus.equals(ArticleEnum.NOT_EXIST_ANY_DATABASE)) {
                message = insertArticle(articleJSON, Content, storePath.toString(), channelname, accessParameters, articleStatus.getCode());
            } else if (articleStatus.equals(ArticleEnum.EXIST_IN_TOM_ARTICLE)) {
                message = insertArticle(articleJSON, Content, storePath.toString(), channelname, accessParameters, articleStatus.getCode());
            } else {
                message = updateArticle(articleJSON, Content, storePath.toString(), channelname, accessParameters, articleStatus.getCode());
            }
            if (message == 1) {
                FileUtils.write(new File(storePath.toString()), json, StandardCharsets.UTF_8);
                articleMapper.insertlog("成功产生的json目录：" + storePath, ID);
            } else {
                return "数据库执行失败";
            }
        } catch (IOException e) {
            log.error("保存json文件出错", e);
        }

        if ("2".equals(provenance)) {
            return "新增成功";
        }
        return "更新成功";
    }


    /**
     * 根据期刊查询数据插入到opencms库的tom_prime_periodical表
     *
     * @param magazineIssueNumber 期刊编号
     * @return 响应体
     */
    @Override
    public CustomResponse insertTomPrimePeriodical(Integer magazineIssueNumber) {
        try {
            TomPrimePeriodical tomPrimePeriodical1 = primePeriodicalMapper.selectByjournalNum(String.valueOf(magazineIssueNumber));

            if (tomPrimePeriodical1 != null) {
                return CustomResponse.ok();
            }

            HttpRequest Request = HttpRequest.post(getMagazineList);
            Request.timeout(1000 * 60);
            Request.form("SiteNo", "10")
                    .form("IssueNumber", magazineIssueNumber);
            HttpResponse resp = Request.send();
            Object extract = JSONPath.extract(resp.bodyText(), "$.JData");
            List<TomPrimePeriodical> MagazineList = ((JSONArray) extract).toJavaList(TomPrimePeriodical.class);
            if (MagazineList.size() <= 0) {
                return CustomResponse.fail().body("explain", "请检查期刊号是否正确");
            }
            TomPrimePeriodical tomPrimePeriodical = MagazineList.get(0);
            tomPrimePeriodical.setJournalNum(tomPrimePeriodical.getIssueNumber());
            tomPrimePeriodical.setCreateTime(new Date());
            tomPrimePeriodical.setIsPeriodical(1);
            primePeriodicalMapper.updat_tom_prime_periodical();

            tomPrimePeriodical1 = primePeriodicalMapper.selectByjournalNum(String.valueOf(magazineIssueNumber));
            if (tomPrimePeriodical1 != null) {
                //修改
                primePeriodicalMapper.updateByPrimaryKeySelective(tomPrimePeriodical);
            } else {
                //新增
                primePeriodicalMapper.insertSelective(tomPrimePeriodical);
            }

        } catch (Exception e) {
            log.error("插入tom_prime_periodical表报错====>" + e);
        }
        return CustomResponse.ok();
    }

    /**
     * 生成json对象
     *
     * @param articleContents  文章详情
     * @param channelname      频道名
     * @param accessParameters 访问文章详情的接口
     * @param DataTab          类型（=4为付费文章，否则为免费文章）
     * @return 生成的文章JSON内容
     */
    @Override
    public ArticleJSON createJSON(ArticleContentDTO articleContents, String channelname, String
            accessParameters, String DataTab) {
        ArticleJSON articlejson = new ArticleJSON();
        //网址<>
        String URL;
        String rawUrl = articleContents.getJData().getArticle().getLinkUrl();
        if (StringUtils.isBlank(rawUrl)) {
            URL = "";
        } else {
            if (rawUrl.contains("https://www.businessweekly.com.tw")) {
                URL = rawUrl;
            } else {
                URL = "https://www.businessweekly.com.tw" + rawUrl;
            }
        }
        log.info("正在生成json:{}", URL);

        articlejson.setUrl(Collections.singletonList(URL));
        articlejson.setAccessParameters(accessParameters);
        //主题
        articlejson.setTitle(articleContents.getJData().getArticle().getWebTitle());
        //圖片路徑
        List<ArticleContentDTO.JDataBean.DSImgBean> dsImgList = articleContents.getJData().getDSImg();
        // DSImg
        articlejson.setDSImg(dsImgList);
        List<String> images = new ArrayList<>();
        for (ArticleContentDTO.JDataBean.DSImgBean dsImg : dsImgList) {
            String rawImages = dsImg.getImgSrc();
            images.add(rawImages);
        }
        articlejson.setImages(images);

        //压缩图片
        List<String> Compress = new ArrayList<>();
        for (String image : images) {
            String s = service.saveImage(image, channelname);
            if (!"".equals(s)) {
                Compress.add(s);
            } else {
                return null;
            }
        }
        String collect = String.join(",", Compress);
        articlejson.setThumbnail(Collections.singletonList(collect));

        //內文
        String WebAbstract = "<div class='articlebody col-md-12 be-changed'>" +
                "<summary class='summary'><p>" + articleContents.getJData().getArticle().getWebAbstract() + "</p></summary><p></p>";//拼接摘要
        List<String> textList = new ArrayList<>();
        //取到文章正文
        String text = "<div class='articlebody col-md-12 be-changed'>" + articleContents.getJData().getArticle().getWebContent()
                .replaceAll("src=\"//ibw.bwnet.com.tw/", "src=\"https://ibw.bwnet.com.tw/")
                .replaceAll("/opencms/sites/www.businessweekly.com.tw/AC_Gallery", "http://ibw.bwnet.com.tw/AC_GalleryAC_Gallery");
        //将文件正文里面的图片占位符做替换
        for (ArticleContentDTO.JDataBean.DSImgBean daImg : dsImgList) {
            if (text.contains(daImg.getDSTagName())) {
                String imgurl = service.saveImage(daImg.getImgSrc(), channelname);
                if (StringUtil.isEmpty(imgurl) && StringUtil.isNotEmpty(daImg.getImgSrc())) {
                    return null;
                }
                String after = "<figure class='articlephoto'> \n " +
                        " <img class='lazy'  data-original='" + daImg.getImgSrc() + "' " +
                        "alt='" + daImg.getTitle() + "' " +
                        "src='" + imgurl + "'> \n " +
                        "<figcaption  class='photocaption small'>"
                        + daImg.getDescription();
                if (StringUtils.isNotBlank(daImg.getAuthor())) {
                    after += "("
                            + daImg.getAuthor()
                            + ")";
                }
                after += "</figcaption> \n </figure>";
                text = text.replace(daImg.getDSTagName(), after);
            }
        }
        List<ArticleContentDTO.JDataBean.DSBoxBean> dsBoxList = articleContents.getJData().getDSBox();
        // DSBox
        articlejson.setDSBox(dsBoxList);
//        for (ArticleContent.JDataBean.DSBoxBean dsbox : dsBoxList) {
//            if (text.contains(dsbox.getDSTagName())) {
//                String after = "<div class='relatedinfo'><h3>" + dsbox.getTopic() + "</h3> <p>" +
//                        dsbox.getContent() + "</div>";
//                text = text.replace(dsbox.getDSTagName(), after);
//            }
//        }
        text = text.replace("{DS}", "");
        String xml11pattern = "[^"
                + "\u0001-\uD7FF"
                + "\uE000-\uFFFD"
                + "\ud800\udc00-\udbff\udfff"
                + "]+";
        String xml10pattern = "[^"
                + "\u0009\r\n"
                + "\u0020-\uD7FF"
                + "\uE000-\uFFFD"
                + "\ud800\udc00-\udbff\udfff"
                + "]";

        String legal = text.replaceAll(xml11pattern, "");
        String cc = legal.replaceAll(xml10pattern, "");
        textList.add(cc);
        articlejson.setText(textList);
        //todo setTbfalse   还不知道是什么
        ArrayList<String> tbfalselist = new ArrayList<>();
        tbfalselist.add("");
        articlejson.setTbfalse(tbfalselist);

        articlejson.setWriter(articleContents.getJData().getArticle().getWriter());
        //作者
        //如果是广告，取writer字段
        if (articleContents.getJData().getArticle().getIsAD()) {
            articlejson.setAuthor(articleContents.getJData().getArticle().getWriter());
        } else {
            articlejson.setAuthor(articleContents.getJData().getArticle().getOriginalWriter());
        }
        //来自什么地方
        String from = "";
        switch (articleContents.getJData().getSpecialTopic().getSiteNo()) {
            case 10:
                from = "台湾商周网";
                break;
            case 16:
                from = "IOS-APP";
                break;
            case 17:
                from = "Andriod-APP";
                break;
        }
        articlejson.setFrom(from);
        //sourceUrl原链接
        articlejson.setSourceUrl(URL);
        //摘要
        articlejson.setSummary(articleContents.getJData().getArticle().getWebAbstract());
//        if (StringUtils.isNotBlank(articleContents.getJData().getArticle().getWebAbstract())) {
//            articlejson.setSummary(articleContents.getJData().getArticle().getWebAbstract());
//        } else {
//            articlejson.setSummary(articleContents.getJData().getArticle().getWebIntroduction());
//        }

        //时间
        Date newDataTime = null;
        Date realPublishTime = null;
        try {
            newDataTime = DateUtils.parseDate(articleContents.getJData().getArticle().getNewDataTime(), "yyyy-MM-dd'T'HH:mm:ss");
            realPublishTime = DateUtils.parseDate(articleContents.getJData().getArticle().getRealPublishTime(), "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            log.error("");
        }
        articlejson.setRelease(DateFormatUtils.format(newDataTime, "yyyy-MM-dd HH:mm"));
        articlejson.setRealPublishTime(DateFormatUtils.format(realPublishTime, "yyyy-MM-dd HH:mm"));
        //大网文章类型
        articlejson.setContentType(articleContents.getJData().getArticle().getContentType());
        Map<String, String> params = new LinkedHashMap<>();
        for (String param : accessParameters.split("&")) {
            String[] keyValue = param.split("=", 2);
            String key;
            try {
                key = URLDecoder.decode(keyValue[0], "UTF-8");
                String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], "UTF-8") : "";
                if (!key.isEmpty()) {
                    params.put(key, value);
                }
            } catch (UnsupportedEncodingException ignored) {
                // do nothing
            }
        }
        articlejson.setParamSiteNo(params.get("SiteNo"));
        articlejson.setParamArticleNo(params.get("ArticleNo"));
        articlejson.setParamType(params.get("Type"));
        //Keywords 關鍵詞
        StringBuilder Keywords = new StringBuilder();
        List<ArticleContentDTO.JDataBean.ArticleTagBean> articleTag = articleContents.getJData().getArticleTag();
        for (ArticleContentDTO.JDataBean.ArticleTagBean Keyword : articleTag) {
            Keywords.append(" ").append(Keyword.getName());
        }
        articlejson.setKeywords(Keywords.toString());

        // 关键标签
        List<String> keyLabelList = articleContents.getJData().getKeyLabelList();
        articlejson.setKeyLabel(String.join(",", keyLabelList));

        //"downImagesFlag":"false",
        articlejson.setDownImagesFlag("false");
        //"checked":"true",
        articlejson.setChecked("true");


        //所有的content_json文件都需要加上CategroyName,CategroyTitle,SubCategoryName.SubCategoryTitle字段
        articlejson.setCategroyName(articleContents.getJData().getArticle().getCategoryID()); //一级栏位
        articlejson.setCategroyTitle(articleContents.getJData().getArticle().getCategoryName());
        articlejson.setSubCategoryName(articleContents.getJData().getArticle().getSubCategoryID());//二级栏位
        articlejson.setSubCategoryTitle(articleContents.getJData().getArticle().getSubCategoryName());


        //"类型"获取作者，是廣編企劃就是3<商周>否则就是1<免费>
        if (DataTab.equals("4")) {
            log.info("-----------------付費文章生成:{}", articleContents.getJData().getArticle().getID());
            articlejson.setType("2");
            if (articleContents.getJData().getArticle().getMagazineName().equals("alive")) {
                articlejson.setCategroyName("0000000110"); //一级栏位
                articlejson.setCategroyTitle("alive");
                articlejson.setSubCategoryName(articleContents.getJData().getArticle().getCategoryID());//二级栏位
                articlejson.setSubCategoryTitle(articleContents.getJData().getArticle().getCategoryName());
            } else {
                articlejson.setCategroyName(articleContents.getJData().getArticle().getCategoryID()); //一级栏位
                articlejson.setCategroyTitle(articleContents.getJData().getArticle().getCategoryName());
                articlejson.setSubCategoryName(articleContents.getJData().getArticle().getSubCategoryID());//二级栏位
                articlejson.setSubCategoryTitle(articleContents.getJData().getArticle().getSubCategoryName());
            }
            articlejson.setPoints(String.valueOf(articleContents.getJData().getArticle().getPoints()));
        } else {
            articlejson.setType(articleContents.getJData().getArticle().getIsAD() ? "3" : "1");
            //数据原生稿，又是付费 145 默认付费
            if (DataTab.equals("5") && articleContents.getJData().getArticle().getCategoryID() != null && articleContents.getJData().getArticle().getCategoryID().equals("145")) {
                articlejson.setType("2");
                articlejson.setCategroyName(articleContents.getJData().getArticle().getCategoryID()); //一级栏位
                articlejson.setCategroyTitle(articleContents.getJData().getArticle().getCategoryName());
                articlejson.setSubCategoryName(articleContents.getJData().getArticle().getSubCategoryID());//二级栏位
                articlejson.setSubCategoryTitle(articleContents.getJData().getArticle().getSubCategoryName());
                //articlejson.setMagazineSinglePrice(String.valueOf(articleContents.getJData().getArticle().getMagazineSinglePrice()));
                articlejson.setPoints("30");//String.valueOf(articleContents.getJData().getArticle().getPoints())
            }
        }
        //杂志合刊号期数
        articlejson.setNumber(articleContents.getJData().getArticle().getMagazineIssueNumber());
        // authorImg作者图片
        String downloadImgURL = service.saveImage(articleContents.getJData().getArticleAuthor().getImgSrc(), channelname);
        if (StringUtils.isEmpty(downloadImgURL) && StringUtils.isNotEmpty(articleContents.getJData().getArticleAuthor().getImgSrc())) {
            return null;
        }
        articlejson.setAuthorImg("<img class='lazy' data-original='" + articleContents.getJData().getArticleAuthor().getImgSrc()
                + "'alt='" + articleContents.getJData().getArticleAuthor().getTitle()
                + "' src='" + downloadImgURL + "'>");
        //authorSource出版来源
        articlejson.setAuthorSource(articleContents.getJData().getArticleAuthor().getTitle());
        //  smallTitle小标题
        articlejson.setSmallTitle(articleContents.getJData().getArticle().getWebSubTitle());
        //todo headImg 头像
        String downloadHeadimg = service.saveImage(articleContents.getJData().getArticleImg().getImgSrc(), channelname);
        if (StringUtil.isEmpty(downloadHeadimg) && StringUtil.isNotEmpty(articleContents.getJData().getArticleImg().getImgSrc())) {
            return null;
        }
        String headimg = "<figure class='articlephoto'> \n <img src='" + downloadHeadimg
                + "' alt='" + articleContents.getJData().getArticle().getWebTitle()
                + "'> \n <figcaption class='photocaption small'>\n  <p>"
                + articleContents.getJData().getArticleImg().getDescription();
        if (StringUtils.isNotBlank(articleContents.getJData().getArticleImg().getAuthor())) {
            headimg += "("
                    + articleContents.getJData().getArticleImg().getAuthor()
                    + ")";
        }
        headimg += "</p>\n </figcaption> \n</figure>";
        articlejson.setHeadImg(headimg);
        String articleListImg = service.saveImage(articleContents.getJData().getArticleImg().getImgSrc(), channelname);
        if (StringUtil.isEmpty(downloadHeadimg) && StringUtil.isNotEmpty(articleContents.getJData().getArticleImg().getImgSrc())) {
            return null;
        }
        articlejson.setWebIntroduction(articleContents.getJData().getArticle().getWebIntroduction());//引言
        articlejson.setArticleListImg(articleListImg);
        articlejson.setSitename("/sites/www.businessweekly.com.tw/");
        articlejson.setTypename("myarticle");
        articlejson.setTypeURL("opencms://system/modules/www.businessweekly.com.tw/schemas/Article.xsd/OpenCmsArticleType/");
        //articlejson.setMagazineIssueNumber(articleContents.getJData().getArticle().getMagazineIssueNumber());//杂志期号
        articlejson.setMagazinePageNo(articleContents.getJData().getArticle().getMagazinePageNo());//页码

        // 製作人
        articlejson.setProducer(articleContents.getJData().getArticle().getProducer());
        // Sorter 整理者
        articlejson.setSorter(articleContents.getJData().getArticle().getSorter());
        // Interviewer 採訪者
        articlejson.setInterviewer(articleContents.getJData().getArticle().getInterviewer());
        // Researcher 研究員
        articlejson.setResearcher(articleContents.getJData().getArticle().getResearcher());
        // Translator 譯文者
        articlejson.setTranslator(articleContents.getJData().getArticle().getTranslator());
        // ExtendTitle 延伸欄位-標題
        articlejson.setExtendTitle(articleContents.getJData().getArticle().getExtendTitle());
        // ExtendValue 延伸欄位-內容
        articlejson.setExtendValue(articleContents.getJData().getArticle().getExtendValue());

        // CoverList 系列文章
        // SiteNo=16&Type=3&ArticleNo=3006168
        List<ArticleContentDTO.JDataBean.CoverBean> coverList = articleContents.getJData().getCoverList();
        if (coverList.size() > 0) {
            List<String> coverArray = new ArrayList<>();
            for (ArticleContentDTO.JDataBean.CoverBean coverBean : coverList) {
                coverArray.add("SiteNo=" + coverBean.getSiteNo() + "&Type=" + coverBean.getDataTab() + "&ArticleNo=" + coverBean.getID());
            }
            articlejson.setCoverList(coverArray);
        }

        // ArticleInnerList 延伸閱讀(文中)
        List<ArticleContentDTO.JDataBean.ArticleInnerBean> articleInnerList = articleContents.getJData().getArticleInnerList();
        if (articleInnerList.size() > 0) {
            List<String> articleInnerArray = new ArrayList<>();
            for (ArticleContentDTO.JDataBean.ArticleInnerBean articleInnerBean : articleInnerList) {
                articleInnerArray.add("SiteNo=" + articleInnerBean.getSiteNo() + "&Type=" + articleInnerBean.getDataTab() + "&ArticleNo=" + articleInnerBean.getID());
            }
            articlejson.setArticleInnerList(articleInnerArray);
        }

        // ArticleEndList 延伸閱讀(文末)
        List<ArticleContentDTO.JDataBean.ArticleEndBean> articleEndList = articleContents.getJData().getArticleEndList();
        if (articleEndList.size() > 0) {
            List<String> articleEndArray = new ArrayList<>();
            for (ArticleContentDTO.JDataBean.ArticleEndBean articleEndBean : articleEndList) {
                articleEndArray.add("SiteNo=" + articleEndBean.getSiteNo() + "&Type=" + articleEndBean.getDataTab() + "&ArticleNo=" + articleEndBean.getID());
            }
            articlejson.setArticleEndList(articleEndArray);
        }

        // 延伸閱讀(文中文)
        articlejson.setWebFurtherContent(articleContents.getJData().getArticle().getWebFurtherContent());

        log.info("生成json字符串完成");
        return articlejson;
    }


    /**
     * 新增到数据库
     *
     * @param articleJSON      文章JSON对象
     * @param Content          文章详情
     * @param aid              存在json的路径
     * @param channelname      频道名
     * @param accessParameters 调用文章详情接口时使用的参数
     */
    @Override
    public int insertArticle(ArticleJSON articleJSON, ArticleContentDTO Content, String aid, String
            channelname, String accessParameters, Integer article_source) {
        int message = 1;
        ArticleVO articlesql = new ArticleVO();
        articlesql.setAid(aid);
        String URL;
        String rawUrl = Content.getJData().getArticle().getLinkUrl();
        if (StringUtils.isBlank(rawUrl)) {
            URL = "";
        } else {
            String[] urls = rawUrl.split("/");
            URL = "https://www.businessweekly.com.tw/article.aspx?id=" + urls[urls.length - 1] + "&type=" + urls[urls.length - 2];
        }
        articlesql.setUrl(URL);
        articlesql.setTitle(articleJSON.getTitle());
        //10是web端，16为iOS，andriod为17
        switch ("10") {
            case "10":
                articlesql.setSource("台湾商周网");
                break;
            case "16":
                articlesql.setSource("ios");
                break;
            case "17":
                articlesql.setSource("andriod");
                break;
        }
        articlesql.setSpidertime(new Date());
        articlesql.setSource(String.join(",", articleJSON.getUrl()));
        articlesql.setChannel(channelname);
        articlesql.setKeywords(articleJSON.getKeywords());
        articlesql.setPicnum(articleJSON.getImages().size());
        articlesql.setLanmu(channelname);
        articlesql.setDevice("");
        articlesql.setSourceurl(getArticle);
        articlesql.setCopyright("");
        articlesql.setListurl(getArticleList);
        articlesql.setPartnerId("");
        articlesql.setWebconfigId("");
        articlesql.setAccessParameters(accessParameters);
        articlesql.setApiId(Content.getJData().getArticle().getID());
        articlesql.setArticleSource(article_source);
        articlesql.setCreateDate(new Date());
        articlesql.setKeyLabel(articleJSON.getKeyLabel());
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            articlesql.setPubtime(simpleDateFormat.parse(articleJSON.getRelease()));//发布时间
            articlesql.setRealPublishTime(simpleDateFormat.parse(articleJSON.getRealPublishTime()));//真实发布时间
            articleMapper.insert(articlesql);
        } catch (Exception e) {
            message = 2;
            log.error("保存到数据库出错", e);
        }
        return message;
    }

    /**
     * 修改数据库的数据（根据aid做修改）
     *
     * @param articleJSON      拼接的json对象
     * @param Content          接口返回的对象
     * @param aid              json放的位置
     * @param channelname      频道名
     * @param accessParameters 调用文章详情接口时的参数
     */
    @Override
    public int updateArticle(ArticleJSON articleJSON, ArticleContentDTO Content, String aid, String
            channelname, String accessParameters, int articleSource) {
        int message = 1;
        ArticleVO articlesql = new ArticleVO();
        articlesql.setAid(aid);
        String URL;
        String rawUrl = Content.getJData().getArticle().getLinkUrl();
        if (StringUtils.isBlank(rawUrl)) {
            URL = "";
        } else {
            String[] urls = rawUrl.split("/");
            URL = "https://www.businessweekly.com.tw/article.aspx?id=" + urls[urls.length - 1] + "&type=" + urls[urls.length - 2];
        }
        articlesql.setUrl(URL);
        articlesql.setTitle(articleJSON.getTitle());
        //10是web端，16为iOS，andriod为17
        switch ("10") {
            case "10":
                articlesql.setSource("台湾商周网");
                break;
            case "16":
                articlesql.setSource("ios");
                break;
            case "17":
                articlesql.setSource("andriod");
                break;
        }
        articlesql.setSpidertime(new Date());
        articlesql.setSource(String.join(",", articleJSON.getUrl()));
        articlesql.setChannel(channelname);
        articlesql.setKeywords(articleJSON.getKeywords());
        articlesql.setKeyLabel(articleJSON.getKeyLabel());
        articlesql.setPicnum(articleJSON.getImages().size());
        articlesql.setLanmu(channelname);
        articlesql.setDevice("");
        articlesql.setSourceurl(getArticle);
        articlesql.setCopyright("");
        articlesql.setListurl(getArticleList);
        articlesql.setPartnerId("");
        articlesql.setWebconfigId("");
        articlesql.setAccessParameters(accessParameters);
        articlesql.setApiId(Content.getJData().getArticle().getID());
        articlesql.setArticleSource(articleSource);
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            articlesql.setPubtime(simpleDateFormat.parse(articleJSON.getRelease()));//发布时间
            articlesql.setRealPublishTime(simpleDateFormat.parse(articleJSON.getRealPublishTime()));//真实发布时间
            articleMapper.updateArticle(articlesql);
        } catch (Exception e) {
            message = 2;
            log.error("保存到数据库出错", e);
        }
        return message;
    }

    @Override
    public void updateArticleAccessParameters(String startDate, String endDate) {
        List<String> tomArticles = tomArticleMapper.selectListAccessParameters(startDate, endDate);
        for (String p : tomArticles) {
            Map<String, String> params = new LinkedHashMap<>();
            // TODO 这个地方要判空
            if (p == null || p.equals("")) {
                continue;
            }
            for (String param : p.split("&")) {
                String[] keyValue = param.split("=", 2);
                String key;
                try {
                    key = URLDecoder.decode(keyValue[0], "UTF-8");
                    String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], "UTF-8") : "";
                    if (!key.isEmpty()) {
                        params.put(key, value);
                    }
                } catch (UnsupportedEncodingException ignored) {
                    // do nothing
                }
            }
            tomArticleMapper.splitAccessParameters(p,params.get("SiteNo"),params.get("Type"),params.get("ArticleNo"));
        }
    }

    /**
     * 判断文章是不是已经录入过
     *
     * @param articleContents 文章详情对象
     * @param provenance      状态（1-更新；2-新增）
     * @return 在能够查找到对应记录，且 provenance 状态为 1，返回 aid 字段，provenance 状态不为 1 时，则返回 1；
     * 当没有对应记录时，则返回 2。
     */
    @Override
    public String JudgeArticleStatus(ArticleContentDTO articleContents, String provenance, String accessParameters) {
        String rawUrl = articleContents.getJData().getArticle().getLinkUrl();
        String newURL;
        String oldURL;
        if (StringUtils.isBlank(rawUrl)) {
            newURL = "";
        } else {
            if (rawUrl.contains("https://www.businessweekly.com.tw")) {
                newURL = rawUrl;
            } else {
                newURL = "https://www.businessweekly.com.tw" + rawUrl;
            }
        }
        if (StringUtils.isBlank(rawUrl)) {
            oldURL = "";
        } else {
            String[] urls = rawUrl.split("/");
            oldURL = "https://www.businessweekly.com.tw/article.aspx?id=" + urls[urls.length - 1] + "&type=" + urls[urls.length - 2];
        }
        String apiId = articleContents.getJData().getArticle().getID();
        String aid = articleMapper.selectUrl(newURL, oldURL, articleContents.getJData().getArticle().getWebTitle(), apiId, accessParameters);
        if (StringUtils.isNotEmpty(aid)) {
            //新增但已存在
            log.info("这篇文章取过了,新的url:{}", rawUrl);
            return aid;//过，不录入这篇文章
        } else {
            //不存在
            log.info("不存在，做新增:{}", rawUrl);
            return null;//新增
        }
    }

    /**
     * 查询opencms是否存在文章路径，
     * 如果存在，将文章访问路径转换为JSON文件路径
     *
     * @param content
     * @param accessParameters
     * @return
     */
    @Override
    public String queryPathInOpencms(ArticleContentDTO content, String accessParameters) {
        // 1. 在opencms中查询文章路径
        String articlePath = tomArticleMapper.selectArticle(content.getJData().getArticle().getWebTitle(), accessParameters);
        if (articlePath == null || "".equals(StringUtils.trim(articlePath))) {
            return null;
        }

        // 2. 根据文章访问路径推算出所在JSON文件路径
        String jsonPath = articlePath.replace("/sites/www.businessweekly.com.tw/", prefix)
                .replace("html", "json")
                .replace("article", "content")
                .replace("archive", "content");

        return jsonPath;
    }

}
